<template>
  <div class="content_box">
    <TableModel :table-data="roleList" :loading="loading" :is-show-select=false>
      <el-table-column prop="rolename" label="角色/工种名称"></el-table-column>
      <el-table-column prop="roledesc" label="角色/工种描述"></el-table-column>
      <el-table-column prop="operator" label="创建人"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteRoleInfo(scope.row.roleid)">删除</el-button>
        </template>
      </el-table-column>
    </TableModel>
    <Pagination :pageData="pageData" @pageNum="pageNumData" @pageSize="pageSizeData"></Pagination>
  </div>
</template>

<script>
import Pagination from "../Pagination";
import TableModel from "../../models/TableModel";
import HttpInterface from "../../../util/httpInterface";
import { messageBox } from "../../../util/utils";

export default {
  name: "RoleList",
  components: {
    Pagination, TableModel
  },
  data() {
    return {
      loading: false,
      roleList: [],
      pageData: {
        total: 0,
        pageNum: 1,
        pageSize: 20
			}
    };
  },

  methods: {
    pageNumData (page) {
      this.pageData.pageNum = Number(page);
      this.getRoleList();
    },

    pageSizeData (page) {
      this.pageData.pageSize = Number(page);
      this.getRoleList();
    },

    // 获取角色列表
    getRoleList () {
      this.loading = true;
      let _this = this;
      let params = {
        pageSize: this.pageData.pageSize,
        pageNum: this.pageData.pageNum
      }
      HttpInterface.requestGet(this, HttpInterface.URL_ROLE_LIST, params, function (res) {
        var { data, total } = res.data;
        _this.pageData.total = total;
        _this.roleList = data.reverse();
        _this.loading = false;
      }, function (err) {
        _this.loading = false;
        _this.errorMessage('加载失败！');
      })
    },

    handleEdit(row) {
      this.$emit("modifRole", row.roleid);
    },

    deleteRoleInfo(roleid) {
      var _this = this;
      messageBox(() => {
        var params = { roleid };
        HttpInterface.requestPost(this, HttpInterface.URL_ROLE_DEL_ROLE, params, function (res) {
          var data = res.data;
          if (data.success) {
            _this.getRoleList();
            _this.successMessage("删除成功！");
          } else {
            _this.errorMessage("删除失败！");
          }
        }, function (err) {
          _this.errorMessage("删除失败！");
        }, true);
      })
    }
  }
};
</script>

<style  scoped>
</style>
